package com.java.base.exercise.current;

import java.util.concurrent.CountDownLatch;

/**
 * @Author RenPu
 * @Date 2021/8/10 10:03
 * @Version 1.0
 * @Description: 线程计数器: 模拟A任务需要，在二个任务完成后，再进行执行的场景
 **/
public class CountDownLatchTitle {

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch latch = new CountDownLatch(2);

        System.out.println("============ddddd========="+latch.getCount());
        new Thread() {
            @Override
            public void run() {
                System.out.println("子线程" + Thread.currentThread().getName() + "正在执行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("11111111子线程" + Thread.currentThread().getName() + "执行完毕");
                latch.countDown();
            }
        }.start();


        new Thread() {
            @Override
            public void run() {
                System.out.println("子线程" + Thread.currentThread().getName() + "正在执行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("222222222子线程" + Thread.currentThread().getName() + "执行完毕");
                latch.countDown();
            }
        }.start();


        System.out.println("等待 2 个子线程执行完毕...");
        latch.await();
        System.out.println("2 个子线程已经执行完毕");
        System.out.println("继续执行主线程");


    }


}

